package com.mobartisan.vehiclenetstore;

import android.app.Application;
import android.app.Dialog;
import android.os.Handler;
import android.os.Message;

import com.mobartisan.vehiclenetstore.getui_push.InformationReceiver;
import com.mobartisan.vehiclenetstore.getui_push.NotificitionManager;
import com.mobartisan.vehiclenetstore.ui.activity.MainActivity;
import com.mobartisan.vehiclenetstore.ui.base.BaseActivity;
import com.mobartisan.vehiclenetstore.util.SharedPreferencesUtil;


/**
 * Created by dell on 2016/11/4.
 */

public class ECarApplication extends Application {
    private static final String TAG = "ECarApplication";
    private static ECarApplication context;
    public static Dialog dialog= null;
    private static String url = "";

    public static ECarApplication getInstance() {
        return context;
    }

    private static DemoHandler handler;
    public static MainActivity mainActivity;
    private static boolean activityVisible;
    public static BaseActivity baseActivity;
    String contents;
    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView == null)
     */
    public static String payloadData;

    public static boolean isActivityVisible() {
        return activityVisible;
    }

    public static void activityResumed() {
        activityVisible = true;
    }

    public static void activityPaused() {
        activityVisible = false;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
//        initializeImageLoder(this);
        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(this);
        SharedPreferencesUtil.setInstance(sharedPreferencesUtil);
        if (handler == null) {
            handler = new DemoHandler();
        }
    }

    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }

    public void exitApp() {

        System.gc();
        System.runFinalization();
        System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public static class DemoHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    payloadData = msg.obj.toString();
                    showWay();
                    break;
            }
        }
    }

    private static void showWay() {
        if (isActivityVisible()) {
//            baseActivity.dismissDilog();
//            baseActivity.showDilog(payloadData);
            NotificitionManager.showNofi(context, url,"提醒", payloadData, InformationReceiver.class);
        } else {
            NotificitionManager.showNofi(context, url,"提醒", payloadData, InformationReceiver.class);
        }
    }

}
